/*********************************************************************
 *                                                                   *
 * Copyright (c) 2007-2008 by Shroggle.com Ltd.                      *
 * All rights reserved.                                              *
 *                                                                   *
 * This computer program is protected by copyright law and           *
 * international treaties. Unauthorized reproduction or distribution *
 * of this program, or any portion of it, may result in severe civil *
 * and criminal penalties, and will be prosecuted to the maximum     *
 * extent possible under the law.                                    *
 *                                                                   *
 *********************************************************************/
package org.grenader.fileutils;

import org.junit.Test;
import org.junit.Assert;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
import org.apache.tools.ant.BuildException;
import org.grenader.fileutils.SplitTask;

import java.io.File;

/**
 * Author: Igor Kanshin (igor). </p> Date: Oct 28, 2009
 */
public class SplitTaskTestCase {

  @Test
  public void testExecute() {
    SplitTask task = new SplitTask();
    /*--------------------------------------------------1245 Gb---------------------------------------------------*/
    task.setSize("1245GB");
    Assert.assertEquals(1245 * 1024 * 1024 * 1024L, task.getBytes());

    task.setSize("1245Gb");
    Assert.assertEquals(1245 * 1024 * 1024 * 1024L, task.getBytes());

    task.setSize("1245gb");
    Assert.assertEquals(1245 * 1024 * 1024 * 1024L, task.getBytes());

    task.setSize("1245g");
    Assert.assertEquals(1245 * 1024 * 1024 * 1024L, task.getBytes());

    /*--------------------------------------------------1245 Gb---------------------------------------------------*/


    /*--------------------------------------------------1000 Mb---------------------------------------------------*/
    task.setSize("1000MB");
    Assert.assertEquals(1000 * 1024 * 1024, task.getBytes());

    task.setSize("1000Mb");
    Assert.assertEquals(1000 * 1024 * 1024, task.getBytes());

    task.setSize("1000mb");
    Assert.assertEquals(1000 * 1024 * 1024, task.getBytes());

    task.setSize("1000m");
    Assert.assertEquals(1000 * 1024 * 1024, task.getBytes());

    /*--------------------------------------------------1000 Mb---------------------------------------------------*/

    /*--------------------------------------------------30 b---------------------------------------------------*/
    task.setSize("30b");
    Assert.assertEquals(30, task.getBytes());

    task.setSize("30B");
    Assert.assertEquals(30, task.getBytes());

    task.setSize("30");
    Assert.assertEquals(30, task.getBytes());

    /*--------------------------------------------------30 Mb---------------------------------------------------*/


    /*---------------------------------------------------10 Kb----------------------------------------------------*/
    task.setSize("10KB");
    Assert.assertEquals(10 * 1024L, task.getBytes());

    task.setSize("10Kb");
    Assert.assertEquals(10 * 1024L, task.getBytes());

    task.setSize("10kb");
    Assert.assertEquals(10 * 1024L, task.getBytes());

    task.setSize("10k");
    Assert.assertEquals(10 * 1024L, task.getBytes());

    /*---------------------------------------------------10 Kb----------------------------------------------------*/

    /*-------------------------------------------------10 bytes---------------------------------------------------*/
    task.setSize("10B");
    Assert.assertEquals(10, task.getBytes());

    task.setSize("10b");
    Assert.assertEquals(10, task.getBytes());

    task.setSize("10");
    Assert.assertEquals(10, task.getBytes());

    /*-------------------------------------------------10 bytes---------------------------------------------------*/

    task.setSize("100,2KB");
    Assert.assertEquals(100 * 1024, task.getBytes());


    task.setSize("1054.2KB");
    Assert.assertEquals(1054 * 1024, task.getBytes());
  }

  @Test
  public void testSetSize_null() {
    SplitTask task = new SplitTask();

    task.setSize(null);
    assertEquals(-1, task.getBytes());
  }

  @Test(expected = BuildException.class)
  public void testSetSize_wrongParam() {
    SplitTask task = new SplitTask();

    task.setSize("adfasdfwea");
  }

  @Test public void testExecute_smallFile() {
    SplitTask task = new SplitTask();

    task.setFile(new File("C:/!/num.txt"));
    task.setBytes(35);
    task.setOutputdir(new File("c:/!/res/"));
    task.setPrefix("res-file-part-");

    task.execute();
  }

  @Test public void testExecute_offsetMoreThanBuffer() {
    SplitTask task = new SplitTask();

    task.setFile(new File("C:/!/anttask.zip"));
    task.setBytes(1024);
    task.setOutputdir(new File("c:/!/res/"));
    task.setPrefix("ant-part-");

    task.execute();
  }

  @Test
  public void testGetSectionSuff() {
    SplitTask task = new SplitTask();

    assertEquals("AA", task.getSectionSuff(0));
    assertEquals("AB", task.getSectionSuff(1));
    assertEquals("AZ", task.getSectionSuff(25));
    
    assertEquals("BA", task.getSectionSuff(26));
    assertEquals("BB", task.getSectionSuff(27));

    assertEquals("EG", task.getSectionSuff(110));
  }

  @Test
  public void testExecute_bigFile() {
    SplitTask task = new SplitTask();

    task.setFile(new File(
        "C:\\Monique\\Armarara\\Files-from-server\\2009-11-06\\public_html.clean-2009-11-06.zip"));
    task.setSize("10m");
    task.setOutputdir(new File("c:/!/res/"));
    task.setPrefix("public_html-part8-");

    task.execute();
  }

  @Test @Ignore  
  public void testExecute_bigFile2() {
    SplitTask task = new SplitTask();

    task.setFile(new File(
        "C:\\Monique\\Armarara\\Files-from-server\\2009-11-06\\public_html.full-2009-11-06.tar.bz2"));
    task.setSize("10m");
    task.setOutputdir(new File("c:/!/res/"));
    task.setPrefix("public_html-part9-");

    task.execute();
  }


}

